package com.wu.Backstage.controller;

import com.wu.Backstage.check.MiddlelayerCheck;
import com.wu.Backstage.tool.Tool;
import com.wu.Backstage.util.MailUtil;
import com.wu.Backstage.util.RedisUtil;
import com.wu.Backstage.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;


@Controller
@RequestMapping(value = "/Redis")
public class redisController {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private MiddlelayerCheck oftenCheckf;

    private Tool tools;

    @RequestMapping(value = "/getCode")
    @ResponseBody
    @CrossOrigin
    public void getCode(String email) {
        /**防止用户频繁发送***/
        Boolean aboolen = redisUtil.hasKey(email);
        if (aboolen) {
            tools = new Tool(
                    false,
                    404,
                    "验证码已发送,请勿重复发送！",
                    null);
            oftenCheckf.Tres(tools);
            return;
        }
        Utils send = new Utils();
        String code = send.getcode();
        MailUtil munit = new MailUtil(email,code);
        munit.run();
        ValueOperations valueOperations = redisTemplate.opsForValue();
        System.out.println("发送："+email+' '+code);
        valueOperations.set(email,code ,20, TimeUnit.SECONDS);
        tools = new Tool(true,
                200,
                "验证码发送成功！",
                null);
        oftenCheckf.Tres(tools);
        return;
    }

    @RequestMapping(value = "/verification")
    @ResponseBody
    @CrossOrigin
    public void getCode(String email, String code) {
        Boolean aboolen = redisUtil.hasKey(email);

        if (!aboolen) {
            oftenCheckf.Tres(new Tool(
                    false,
                    404,
                    "验证码已经过期或者未获取验证码,请重试！",
                    null));
            return;
        }
        String str = redisUtil.getStr(email);
        String strs= str.replace("\"", "");
        if (code.equals(strs)||code==strs) {
            redisUtil.del(email);
            oftenCheckf.Tres( new Tool(
                    true,
                    200,
                    "验证成功！",
                    null));
            return;
        }
        oftenCheckf.Tres(new Tool(
                false,
                404,
                "验证码输入错误！",
                null));
    }


    @RequestMapping(value = "/frequency")
    @ResponseBody
    public String counter(String key)
    {
        Long increment = redisUtil.hincrInt(key);
        if(increment >=6){
            redisUtil.del(key);
            oftenCheckf.upadteStat(key);
            return "您的密码连续六次输入错误账号已被锁定,请点击忘记密码进行修改!";
        }
        return "您已经连续输入错误"+increment+"次,连续输入错误六次账号将被锁定!";

    }



}
